import java.util.StringTokenizer;


class P10508_39643 {

	static String readLn() {
		String newLine = System.getProperty("line.separator");
		StringBuffer buffer = new StringBuffer();
		int car = -1;
		try {
			car = System.in.read();
			while ((car > 0) && (car != newLine.charAt(0))) {
				buffer.append((char) car);
				car = System.in.read();
			}
			if (car == newLine.charAt(0))
				System.in.skip(newLine.length() - 1);
		} catch (java.io.IOException e) {
			return (null);
		}
		if ((car < 0) && (buffer.length() == 0))
			return (null);
		return (buffer.toString()).trim();
	}
	
	public static void main(String[] args) {
		
		String line = readLn();
		
		while (line != null) {
			
			StringTokenizer st = new StringTokenizer(line);
			
			int n = Integer.parseInt(st.nextToken());
			int l = Integer.parseInt(st.nextToken());
			
			process(n, l);
			
			line = readLn();
			
		}
		
	}

	private static void process(int n, int l) {
		
		String first = readLn();
		String last = readLn();
		
		
		String[] array = new String[n-2];
		int[] differences = new int[n-2];
		
		for (int k = 0; k < n-2; k++) {
			array[k] = readLn();
			
			int difference = 0;
			for (int j = 0; j < l; j++) {
				if (first.charAt(j) != array[k].charAt(j)) difference++;
			}
			differences[k] = difference;
			
		}
		
		sort(array, differences, 0, n-3);
		
		
		System.out.println(first);
		
		for (int k = array.length - 1; k >= 0; k--) {
			System.out.println(array[k]);
		}
		System.out.println(last);
		
	}
	
    static void sort(String[] array, int[] differences, int lo0, int hi0) {
		int lo = lo0;
		int hi = hi0;
		int mid;

		if (hi0 > lo0) {

			mid = differences[(lo0 + hi0) / 2];

			while (lo <= hi) {
				while ((lo < hi0) && ((differences[lo] > mid)))
					++lo;

				while ((hi > lo0) && ((differences[hi] < mid)))
					--hi;

				if (lo <= hi) {
					swap(array, differences, lo, hi);

					++lo;
					--hi;
				}
			}

			if (lo0 < hi)
				sort(array, differences, lo0, hi);

			if (lo < hi0)
				sort(array, differences, lo, hi0);

		}
	}


	private static void swap(String[] array, int[] teams, int lo, int hi) {
		int aux = teams[lo];
		teams[lo] = teams[hi];
		teams[hi] = aux;
		
		String auxS = array[lo];
		array[lo] = array[hi];
		array[hi] = auxS;
	}

	
}
